package com.chixing.dao.impl;

import com.chixing.dao.EmployeeDao;
import com.chixing.entity.Employee;
import com.chixing.util.DBConnection;

import java.sql.*;
import java.util.List;

public class EmployeeImpl implements EmployeeDao {
    private JDBCTemplate<Employee> jdbcTemplate = new JDBCTemplate<>(new EmployeeRowMapper());
    @Override
    public int insert(Employee employee) {
        String sql = "insert into employee values(?,?,?,?,?,?,?,?)";
        Object[] args = {employee.getEmpno(), employee.getEmpname(), employee.getEmpJob(), employee.getEmpHiredate(),
                employee.getManagerNo(), employee.getEmpSalary(), employee.getEmpComm(), employee.getDeptNo()};
         return jdbcTemplate.update(sql, args);
    }

    @Override
    public int update(Employee employee) {
        String sql = "update employee set ename = ?,job = ?,hiredate = ?,mgr = ?,salary = ?,comm = ?,deptno = ? where empno = ?";
        Object[] args = {employee.getEmpname(), employee.getEmpJob(), employee.getEmpHiredate(), employee.getManagerNo(),};
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public int delete(int empno) {
        String sql = "delete from employee where empno = ?";
        return jdbcTemplate.update(sql, empno);
    }

    @Override
    public Employee selectById(int empno) {
        String sql = "select empno,ename,job , hiredate,mgr,salary,comm, deptno from employee where empno = ?";
        return jdbcTemplate.selectForObject(sql, empno);
    }

    @Override
    public List<Employee> selectAll() {
        String sql = "select empno,ename,job , hiredate,mgr,salary,comm, deptno from employee";
        return jdbcTemplate.selectForList(sql);
    }
}
